<template>
  <div>
    <!-- 顶部tab栏目去掉了mui-fullscreen这个类 -->
    <div id="slider" class="mui-slider">
      <div
        id="sliderSegmentedControl"
        class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted"
      >
        <div class="mui-scroll">
          <a
            :class="['mui-control-item', item.id==0?'mui-active':'']"
            v-for="item in allCategory"
            :key="item.id"
            @click="getPhotoListByCateId(item.id)"
          >{{item.title}}</a>
        </div>
      </div>
    </div>
    <!-- 图片列表区域 -->
    <ul class="photo-list">
      
      <router-link v-for="item in list" :key="item.id" :to="'/home/photoinfo/'+item.id" tag="li">
        <img v-lazy="item.img_url">
        <div class="info">
            <div class="info-title">{{item.title}}</div>
            <div class="info-desc">{{item.zhaiyao}}</div>
        </div>
      </router-link>
    </ul>
  </div>
</template>
<script>
// mui.js是非严格模式 打包的bundle.js是启动严格模式。
// 禁用webpack.config.js严格模式  cnpm install babel-plugin-transform-remove-strict-mode
import mui from "@/lib/mui-master/dist/js/mui.min.js";
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      allCategory: [],
      list: [] //图片列表的数组
    };
  },
  methods: {
    getAllCategory() {
      this.$http.get("api/getimgcategory").then(function(res) {
        if (res.body.status !== 0) {
          return Toast("加载图片种类失败");
        }

        // 原数组unshift改变
        res.body.message.unshift({ title: "全部", id: 0 });
        this.allCategory = res.body.message;
        // this.allCategory =res.body.message.unshift({title:'全部',id:0}) 返回的是这个数组长度。
      });
    },
    getPhotoListByCateId(cateId) {
      this.$http.get("api/getimages/" + cateId).then(function(res) {
        if (res.body.status !== 0) {
          return Toast("error");
        }
        this.list = res.body.message;
      });
    }
  },
  //   当组件中的dom元素渲染好并放在页面中去，会执行这个钩子函数。刚进入图片分享时候，滑动条无法正常工作。
  mounted() {
    mui(".mui-scroll-wrapper").scroll({
      deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
    });
  },
  created() {
    this.getAllCategory();
    this.getPhotoListByCateId(0); //默认进入页面主动请求所有图片列表数据
  }
};
</script>   
<style lang="scss" scoped>
* {
  touch-action: pan-y;
}
.photo-list {
  list-style: none;
  padding: 10px;
  margin: 0;
  li {
    background: #ccc;
    text-align: center;
    margin-bottom: 10px;
    box-shadow:0 0 5px #999;
    position: relative; //文字压在图片上定位 
    // 真正显示的图
    img{width: 100%;display: block}
    // 懒加载图片
    img[lazy="loading"] {
      width: 40px;
      height: 300px;
      margin: auto;
      
    }
    .info{
        position: absolute;
        bottom: 0px;
        color: #fff;
        text-align: left;
        background: rgba(0, 0, 0, 0.5);
        max-height: 100px;
        .info-title{font-size: 14px;}
        .info-desc{font-size: 13px;}
    }
  }
}
</style>
